package com.example.ad_project;

import com.google.gson.Gson;
import com.nus.iss.ad.dbhelper.RequestDBHelper;
import com.nus.iss.ad.model.Request;

import android.os.Bundle;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ApproveRequestDetailActivity extends BaseActivity 
implements OnClickListener
{
	RequestDBHelper data=new RequestDBHelper();

	private Request request;
	ListView lv;
	Button buttonApprove;
	Button buttonReject;
	EditText editTextRejectReason;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
		 Bundle extras = getIntent().getExtras();
	        if (extras == null)
	          return;
	        String requestJson = extras.getString("request");
	        
	        Gson gson=new Gson();
	        request=gson.fromJson(requestJson,Request.class);
		    
		    StrictMode.setThreadPolicy(ThreadPolicy.LAX);
		    
		    setContentView(R.layout.activity_approve_request_detail);
		    
		    lv= (ListView) findViewById(R.id.listViewRequestDetail);
		    lv.setAdapter(new RequestDetailAdapter(this, request.getRequestDetails()));
		    
		    buttonApprove=(Button)findViewById(R.id.buttonApproveRequest);
		    buttonApprove.setOnClickListener(this);
		    
		    buttonReject=(Button)findViewById(R.id.buttonRejectRequest);
		    buttonReject.setOnClickListener(this);
		    
		    editTextRejectReason=(EditText)findViewById(R.id.editTextRejectReason);
	}



	@Override
	public void onClick(View v) {
		if(v==buttonApprove){
		data.approveRequest(request.getRequestId());
		
		showDialog("Approve successful",true);
		
		}
		if(v==buttonReject){
			String rejectReason=editTextRejectReason.getText().toString();
			
			data.rejectRequest(request.getRequestId(),rejectReason);
			
			showDialog("Reject successful",true);
		}

	}
	
	

}
